home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ue312src.zip / LOCK.C < prev    next >
C/C++ Source or Header  |  1993-04-21  |  4KB  |  178 lines

  1. /*    LOCK:    File locking command routines for MicroEMACS
  2.         written by Daniel Lawrence
  3.                                 */
  4.  
  5. #include <stdio.h>
  6. #include "estruct.h"
  7. #include "eproto.h"
  8. #include "edef.h"
  9. #include "elang.h"
  10.  
  11. #if    FILOCK
  12.  
  13. #if    BSD || WMCS || SUN || XENIX || HPUX8 || HPUX9 || AVIION || USG || AUX
  14. #include <sys/errno.h>
  15. extern int sys_nerr;        /* number of system error messages defined */
  16. extern char *sys_errlist[];    /* list of message texts */
  17. #endif
  18.  
  19. #if    MSC
  20. #include <errno.h>
  21. #else
  22. extern int errno;        /* current error */
  23. #endif
  24.  
  25. char *lname[NLOCKS];    /* names of all locked files */
  26. int numlocks;        /* # of current locks active */
  27.  
  28. /* lockchk:    check a file for locking and add it to the list */
  29.  
  30. lockchk(fname)
  31.  
  32. char *fname;    /* file to check for a lock */
  33.  
  34. {
  35.     register int i;        /* loop indexes */
  36.     register int status;    /* return status */
  37.  
  38.     /* check to see if that file is already locked here */
  39.     if (numlocks > 0)
  40.         for (i=0; i < numlocks; ++i)
  41.             if (strcmp(fname, lname[i]) == 0)
  42.                 return(TRUE);
  43.  
  44.     /* if we have a full locking table, bitch and leave */
  45.     if (numlocks == NLOCKS) {
  46.         mlwrite(TEXT173);
  47. /*                      "LOCK ERROR: Lock table full" */
  48.         return(ABORT);
  49.     }
  50.  
  51.     /* next, try to lock it */
  52.     status = xlock(fname);
  53.     if (status == ABORT)    /* file is locked, no override */
  54.         return(ABORT);
  55.     if (status == FALSE)    /* locked, overriden, dont add to table */
  56.         return(TRUE);
  57.  
  58.     /* we have now locked it, add it to our table */
  59.     lname[++numlocks - 1] = (char *)malloc(strlen(fname) + 1);
  60.     if (lname[numlocks - 1] == NULL) {    /* malloc failure */
  61.         undolock(fname);        /* free the lock */
  62.         mlwrite(TEXT174);
  63. /*                      "Cannot lock, out of memory" */
  64.         --numlocks;
  65.         return(ABORT);
  66.     }
  67.  
  68.     /* everthing is cool, add it to the table */
  69.     strcpy(lname[numlocks-1], fname);
  70.     return(TRUE);
  71. }
  72.  
  73. /*    lockrel:    release all the file locks so others may edit */
  74.  
  75. lockrel()
  76.  
  77. {
  78.     register int i;        /* loop index */
  79.     register int status;    /* status of locks */
  80.     register int s;        /* status of one unlock */
  81.  
  82.     status = TRUE;
  83.     while (numlocks-- > 0) {
  84.         if ((s = xunlock(lname[numlocks])) != TRUE)
  85.             status = s;
  86.         free(lname[numlocks]);
  87.     }
  88.     return(status);
  89. }
  90.  
  91. /* lock:    Check and lock a file from access by others
  92.         returns    TRUE = files was not locked and now is
  93.             FALSE = file was locked and overridden
  94.             ABORT = file was locked, abort command
  95. */
  96.  
  97. xlock(fname)
  98.  
  99. char *fname;    /* file name to lock */
  100.  
  101. {
  102.     register char *locker;    /* lock error message */
  103.     register int status;    /* return status */
  104.     char msg[NSTRING];    /* message string */
  105.  
  106.     /* attempt to lock the file */
  107.     locker = dolock(fname);
  108.     if (locker == NULL)    /* we win */
  109.         return(TRUE);
  110.  
  111.     /* file failed...abort */
  112.     if (strncmp(locker, TEXT175, 4) == 0) {
  113. /*                          "LOCK" */
  114.         lckerror(locker);
  115.         return(ABORT);
  116.     }
  117.  
  118.     /* someone else has it....override? */
  119.     strcpy(msg, TEXT176);
  120. /*                  "File in use by " */
  121.     strcat(msg, locker);
  122.     strcat(msg, TEXT177);
  123. /*                  ", overide?" */
  124.     status = mlyesno(msg);        /* ask them */
  125.     if (status == TRUE)
  126.         return(FALSE);
  127.     else
  128.         return(ABORT);
  129. }
  130.  
  131. /*    xunlock: Unlock a file
  132.         this only warns the user if it fails
  133.                             */
  134.  
  135. xunlock(fname)
  136.  
  137. char *fname;    /* file to unlock */
  138.  
  139. {
  140.     register char *locker;    /* undolock return string */
  141.  
  142.     /* unclock and return */
  143.     locker = undolock(fname);
  144.     if (locker == NULL)
  145.         return(TRUE);
  146.  
  147.     /* report the error and come back */
  148.     lckerror(locker);
  149.     return(FALSE);
  150. }
  151.  
  152. lckerror(errstr)    /* report a lock error */
  153.  
  154. char *errstr;        /* lock error string to print out */
  155.  
  156. {
  157.     char obuf[NSTRING];    /* output buffer for error message */
  158.  
  159.     strcpy(obuf, errstr);
  160.     strcat(obuf, " - ");
  161. #if    BSD || WMCS || SUN || XENIX || HPUX8 || HPUX9 || AVIION || USG || AUX
  162.     if (errno < sys_nerr)
  163.         strcat(obuf, sys_errlist[errno]);
  164.     else
  165.         strcat(obuf, TEXT178);
  166. /*                           "[can not get system error message]" */
  167. #else
  168.     strcat(obuf, "Error # ");
  169.     strcat(obuf, int_asc(errno));
  170. #endif
  171.     mlwrite(obuf);
  172. }
  173. #else
  174. lckhello()    /* dummy function */
  175. {
  176. }
  177. #endif
  178.